<script lang="ts" setup>
import { ref, reactive } from 'vue';
import type { Data } from '@/types';
import { message } from '@/components/XtxUI';
import useStore from '@/store';
import { useUserMedia } from '@vueuse/core';
const member = useStore().member;
const form = reactive({
  mobile: '',
  code: '',
});
const obj = defineProps<{
  userInfo: Data;
  uuid: string;
}>();
// 控制验证码倒计时
const isShowCode = ref(false);
const seconds = ref(60);
let timer = null;
// console.log(obj.userInfo, 'userinfo');
// 发送验证码
const phoneRgx = /^1[3-9]\d{9}$/;
function sendCode() {
  if (!phoneRgx.test(form.mobile)) {
    return message({
      type: 'error',
      text: '手机号格式错误，无法发送验证码',
    });
  }
  message({
    type: 'success',
    text: '可以发送验证码',
  });
  isShowCode.value = true;
  if (isShowCode.value) {
    counterClock();
    member.getCodeBind(form.mobile);
  }
}
// 处理倒计时
function counterClock() {
  timer = setInterval(() => {
    seconds.value = seconds.value - 1;
    if (seconds.value <= 0) {
      clearInterval(timer);
      seconds.value = 60;
      isShowCode.value = false;
    }
  }, 1000);
}
function bindPhone() {
  member.handlerBindPhone({
    unionId: obj.uuid,
    mobile: form.mobile,
    code: form.code,
  });
}
</script>
<template>
  <div class="bind">
    <div class="userinfo" v-if="userInfo">
      <img :src="userInfo.figureurl_1" alt="" />
      <p>
        Hi,{{
          userInfo.nickname
        }}欢迎来到小兔鲜，完成绑定后可以和QQ账号一键登录哦~
      </p>
    </div>
    <div class="field">
      <i class="icon iconfont icon-phone"></i>
      <input
        v-model="form.mobile"
        maxlength="11"
        class="input"
        type="text"
        placeholder="绑定的手机号"
      />
    </div>
    <div class="field">
      <i class="icon iconfont icon-code"></i>
      <input
        v-model="form.code"
        maxlength="11"
        class="input"
        type="text"
        placeholder="短信验证码"
      />
      <span class="code" @click="sendCode" v-if="!isShowCode">发送验证码</span>
      <span class="code" v-else>{{ seconds }}秒后再发送验证码</span>
    </div>
    <myButton type="info" size="large" @click="bindPhone">立即绑定</myButton>
  </div>
</template>
<style lang="less" scoped>
.iconfont {
  width: 40px;
  height: 50px;
  position: absolute;
  line-height: 50px;
  left: 0px;
}
.bind {
  text-align: center;
}
.userinfo {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  p {
    margin-left: 10px;
  }
}
.field {
  position: relative;
  margin: 0 auto;
  width: 320px;
  height: 50px;
  text-align: center;
  margin-bottom: 20px;
}
.input {
  border: 1px solid #e4e4e4;
  width: 320px;
  height: 50px;
  line-height: 50px;
  padding: 0 10px 0 40px;
}
.code {
  position: absolute;
  right: 10px;
  line-height: 50px;
  color: #999;
  cursor: pointer;
}
</style>
